﻿using Sprout.Schedule.Consts;
using Quartz;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Sprout.Schedule.Jobs
{
    //[PersistJobDataAfterExecution]
    //是一个属性，可以添加到Job类中，告诉Quartz不要同时执行给定作业定义（指给定作业类）的多个实例
    [DisallowConcurrentExecution]
    public abstract class LocalJobBase : JobBase, IJob
    {
        /// <summary>
        /// 任务开始
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task Execute(IJobExecutionContext context)
        {
            //设置TaskId
            TaskId = context.JobDetail.JobDataMap.GetLong(JobConst.Task_ID_KEY);

            //开始任务
            await JobRunAsync();
            try
            {
                //执行业务逻辑
                await Execute();
            }
            catch (Exception e)
            {
                await LogAsync($"执行 Execute 异常：{e.Message}", e);
            }
            finally
            {
                //任务完成
                await JobRunEndAsync();
            }
        }

        /// <summary>
        /// 需要实现的方法，具体业务实现逻辑
        /// </summary>
        /// <returns></returns>
        public abstract Task Execute();
    }


}
